package com.tutu.service.impl;

import com.tutu.controller.backend.VerifyInfoController;
import com.tutu.core.api.SystemApiUrls;
import com.tutu.service.VerifyInfoService;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.JsonUtil;
import com.tutu.utils.UnicodeUtil;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * VerifyInfoServiceImpl
 * Created by 涂奕恒 on 2023/10/07 15:38.
 */
@Service
public class VerifyInfoServiceImpl implements VerifyInfoService {

    @Override
    public Map<String, Object> pageByParams(Integer status, String keyWord, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = HttpRequestUtil.getOnlyData(SystemApiUrls.base_prefix + SystemApiUrls.verify_page, "status=" + status + "&keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize);

        Map<String, Object> tempMap;

        for (Object obj : JSONArray.fromObject(map.get("content"))) {
            tempMap = JsonUtil.json2Map(obj);

            VerifyInfoController.tempVerifyInfoMap.put(tempMap.get("accessToken") + "", tempMap);
        }

        return map;
    }

    @Override
    public Map<String, Object> saveInfo(HttpServletRequest request, Integer id, String title, String endTime, Integer status) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑验证信息，ID：" + id + "，标题：" + title + "，过期时间：" + endTime + "，状态：" + status);

        String url = SystemApiUrls.base_prefix + SystemApiUrls.verify_update;

        if (null == id) url = SystemApiUrls.base_prefix + SystemApiUrls.verify_create;

        return HttpRequestUtil.getCompleteData(url, "title=" + UnicodeUtil.unicodeEncode(title) + "&endTime=" + UnicodeUtil.unicodeEncode(endTime) + "&id=" + id + "&status=" + status);
    }

    @Override
    public Map<String, Object> delInfo(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除验证信息，ID：" + id);

        return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.verify_delete, "id=" + id);
    }

    @Override
    public Map<String, Object> updateDeviceInfo(HttpServletRequest request, Integer id, String content) {
        LogInfoServiceImpl.saveLogInfo(request, "更新验证消息的设备信息，ID：" + id + "，内容：" + content);

        return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.verify_updateDeviceInfo, "id=" + id + "&content=" + content);
    }
}